/****************************************************************************
*
* Copyright (c) 2023  C*Core -   All Rights Reserved
*
* THIS SOFTWARE IS DISTRIBUTED "AS IS, " AND ALL WARRANTIES ARE DISCLAIMED,
* INCLUDING MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* PROJECT     : CCFC2011BC
* DESCRIPTION : CCFC2011BC wakeup low level drivers code
* HISTORY     : Initial version.
* @file     wkup_lld.c
* @version  1.1
* @date     2023 - 02 - 20
* @brief    Initial version.
*
*****************************************************************************/
#include "wkpu_lld.h"


/*******************************************************************************
 * @brief      MC_ME_ModeSwitch
 * @param[in]  uint32_t mode_t
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void MC_ME_ModeSwitch(uint32_t mode_t)
{
    uint32_t val = mode_t;

    /* Mode + KEY */
    ME.MCTL.R = (val | 0x00005AF0UL);
    /* Mode + inverted KEY */
    ME.MCTL.R = (val | 0x0000A50FUL);
    /* Wait till Complete */
    while (ME.GS.B.S_MTRANS == 1u)
    {
        ;
    }
}

/*******************************************************************************
 * @brief      WKPU_Config
 * @param[in]  uint32_t lpmode
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void WKPU_Config(uint32_t lpmode)
{
    uint32_t wkup_src = WKPU4_PB1;

    if ((ME_MODE_STOP != lpmode) && (ME_MODE_STANDBY != lpmode))
    {
       return;
    }
        
    /*Set WKUP*/
    WKUP.WISR.R     = 0xFFFFFFFFu;
    WKUP.NSR.R      = 0xFFFFFFFFu;
    /*PC[9] PCR[41]     WKUP[13]*/
       WKUP.WIREER.R   = wkup_src;     /* Enable  Rising - Edge Events      */
    WKUP.WIFER.R    = wkup_src;     /* Wakeup/Interrupt Filter Enable  */
    WKUP.WIPUER.R   = 0x1E7FFFFF;   /* Wakeup/Interrupt Pullup Enable   */
    WKUP.WRER.R     = wkup_src;

    if (ME_MODE_STANDBY == lpmode)
    {
        ME.STANDBY.B.FIRCON = 0u;
        ME.STANDBY.B.FXOSCON = 0u;
        ME.STANDBY.B.MVRON = 0u;
    }
    else
    {
        ME.STOP.B.FIRCON = 1u;
        ME.STOP.B.SYSCLK = 0x01u;
        ME.STOP.B.FXOSCON = 0u;
        ME.STOP.B.MVRON = 1u;
    }
    MC_ME_ModeSwitch(lpmode);
}
